package com.ruoyi.web.controller.business;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.DangKouUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.AuditStatus;
import com.ruoyi.common.enums.DelFlag;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.CangKu;
import com.ruoyi.system.domain.DangKou;
import com.ruoyi.system.domain.DingDan;
import com.ruoyi.system.domain.DingDanDetail;
import com.ruoyi.system.domain.vo.ListDetailByRiQiAndCaiPinReq;
import com.ruoyi.system.service.IDingDanDetailService;
import com.ruoyi.system.service.IDingDanService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.error;

/**
 * <p>
 * 订单信息 前端控制器
 * </p>
 *
 * @author qubaofeng
 * @since 2025-02-16
 */
@RestController
@RequestMapping("/system/dingDan")
public class DingDanController extends BaseController {


    @Resource
    private IDingDanService dingDanService;


    /**
     * 获取用户列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DingDan user) {
        startPage();
        List<DangKou> list = dingDanService.selectDingDanList(user);
        return getDataTable(list);
    }

    /**
     * 新增用户
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody DingDan dingDan) {
        dingDan.setCreateBy(getUsername());
        dingDan.setCreateTime(new Date());
        boolean success = dingDanService.saveDingDan(dingDan);
        return toAjax(success);
    }



    /**
     * 新增用户
     */
    @PostMapping("/shenhe")
    public AjaxResult shenhe(@Validated @RequestBody DingDan user) {
        user.setUpdateBy(getUsername());
        user.setUpdateTime(new Date());
        boolean success = dingDanService.shehe(user);
        return toAjax(success);
    }

    /**
     * 根据用户编号获取详细信息
     */
    @GetMapping(value = { "/", "/{id}" })
    public AjaxResult getInfo(@PathVariable(value = "id", required = false) Long id)
    {
        AjaxResult ajax = AjaxResult.success();
        DingDan user = dingDanService.getDetail(id);
        ajax.put(AjaxResult.DATA_TAG, user);
        return ajax;
    }


    @GetMapping("/listByUserId")
    public AjaxResult listByUserId(@RequestParam(value = "userId") Long userId){
        List<DingDan> list = dingDanService.listByUserId(userId);
        return AjaxResult.success(list);
    }


    @GetMapping("/listDingDanHistory")
    public AjaxResult listDingDanHistory(@RequestParam(value = "userId") Long userId,
                                         @RequestParam(value = "huoPinId", required = false) Long huoPinId,
                                         @RequestParam(value = "timeStart", required = false) String timeStart,
                                         @RequestParam(value = "timeEnd", required = false) String timeEnd){
        List<DingDan> list = dingDanService.listDingDanHistory(userId, huoPinId, timeStart, timeEnd);
        return AjaxResult.success(list);
    }

    @PostMapping("/getDetailByRiQiAndCaiPin")
    public AjaxResult getDetailByRiQiAndCaiPin(@RequestBody ListDetailByRiQiAndCaiPinReq params) {

        List<DingDanDetail> list = dingDanService.getDetailByRiQiAndCaiPin(params.getRiQi(), params.getHuoPinIdList());
        return AjaxResult.success(list);
    }

    @DeleteMapping("{id}")
    public AjaxResult del(@PathVariable(value = "id") Long id) {
        DingDan dingDan = new DingDan();
        dingDan.setId(id);
        dingDan.setDelFlag(DelFlag.DEL.getCode());
        dingDanService.updateById(dingDan);
        return AjaxResult.success();
    }

    @GetMapping("/shangBao/{id}")
    public AjaxResult shangBao(@PathVariable(value = "id") Long id) {
        DingDan dingDan = new DingDan();
        dingDan.setId(id);
        dingDan.setCaoGao(0);
        dingDanService.updateById(dingDan);
        return AjaxResult.success();
    }

    @GetMapping("/cheXiao/{id}")
    public AjaxResult cheXiao(@PathVariable(value = "id") Long id) {
        DingDan dingDan = new DingDan();
        dingDan.setId(id);
        dingDan.setStatus(AuditStatus.CANCEL.getCode());
        dingDanService.updateById(dingDan);
        return AjaxResult.success();
    }

}
